package bjf

import ({{if has_any_body $.Def }}
    "bytes"{{end}}
    "io"
    {{if has_bytes_response $.Def }}"io/ioutil"{{end}}
    "net/http"{{if has_form_data $.Def }}
    form "gitee.com/bjf-fhe/openapi-client-template/golang/helper/form"{{end}}{{if has_json_data $.Def }}
    "encoding/json"{{end}}
)

{{ range $method,$operation := $.Def.Operations}}{{ with get_body_def $operation}}//inputs使用到以下数据类型
type {{ method_name $.Name $method}}Body struct { {{ range $key,$par := .Properties}}
    {{valid_var_name $key}}   {{get_var_type $par}} `{{get_body_tagtype $operation}}:'{{$key}}'`{{end}}
}
{{ end }}{{ with get_response_def $operation}}//response使用到以下数据类型
type {{ method_name $.Name $method}}Response struct { {{ range $key,$par := .Properties}}
    {{valid_var_name $key}}   {{get_var_type $par}} `{{get_body_tagtype $operation}}:'{{$key}}'`{{end}}
}
{{ end }}{{ end }}
{{ range $method,$operation := $.Def.Operations}}
// 路径：{{$.Name}}， 调用方法{{ $method}}
{{ wrap_doc $operation.Summary "// "}}
{{ wrap_doc $operation.Description "// "}}{{ if $operation.Parameters}}
//params要求传输以下属性，没有标注'有默认值'的参数由调用方传输{{ range $key,$par := $operation.Parameters}}
//-{{ $par.Value.Name}}{{if has_default $par}}(有默认值){{end}}：{{ wrap_doc_skip_first $par.Value.Description "// "}}{{ end }}{{end}}{{ if $operation.RequestBody}}
//inputs要求传输以下属性，没有标注'有默认值'的参数由调用方传输{{ range $key,$par := get_body_content $operation}}
//-{{ $key}}{{if has_default $par}}(有默认值){{end}}：{{ wrap_doc_skip_first $par.Value.Description "// "}}{{ end }}{{end}}
{{if input_as_object $.Name $method $operation}}func {{ method_name $.Name $method}}({{ path_input $.Name $method $operation}})({{path_output $.Name $method $operation}},error){
{{else}}func {{ method_name $.Name $method }}({{ expanded_path_input $operation "with_default_value"}})({{path_output $.Name $method $operation}},error){
{{end}}
    var body io.ReadWriter
    var err error
    {{ if $operation.RequestBody}}body = new(bytes.Buffer)
    err = {{get_body_tagtype $operation}}.NewEncoder(body).Encode(input)
    if err == nil {
    {{ end }}
        var parsedUrl = "{{ $.Name}}";
        {{ range $index, $par := path_params $operation }}
        parsedUrl = config.ReplaceParam("{{$par.Name}}",params.{{valid_var_name $par.Name}}){{end}}
        var request *http.Request
        request,err = http.NewRequest("{{$method}}",parsedUrl,body)
        if err == nil {

            var response *http.Response
            response,err = client.Do(request)

            if err == nil { {{with path_output $.Name $method $operation}}
                {{if eq . "[]byte"}}
                return ioutil.ReadAll(response.Body)
                {{else}}
                var result = new({{trim_prefix . "*"}})
                err = {{get_response_tagtype $operation}}.NewDecoder(response.Body).Decode(result)
                return result,err
                {{end}}
                {{end}}
            }
        }
    {{ if $operation.RequestBody}}
    }
    {{end}}
    return nil,err
}{{ end }}